// dbghelp.dll dynamic import

// ----------------------------------------------------------------------------

#include "stdafx.h"

#include "DSymLoadError.h"

#include "DSymLoadDbgHelp.h"

// ----------------------------------------------------------------------------

namespace DSymLoad
{

// ----------------------------------------------------------------------------

CDbgHelp::CDbgHelp(const CStringA &csLibPath)
{
    CPathA LibPath(csLibPath);
    LibPath.Append(GetLibraryFileName());

    m_hModule = ::LoadLibraryA(LibPath);
    if (!m_hModule)
        throw MakeFunctionCallError("LoadLibrary", CStringA("\"") + LibPath.m_strPath + CStringA("\""));

#define DBGHELP_GET_PROC_ADDRESS(x) \
    m_p##x = reinterpret_cast<Ptr##x>(::GetProcAddress(m_hModule, #x)); \
    if (!m_p##x) \
    { \
        ::FreeLibrary(m_hModule); \
        throw CError("Routine " #x " not found (dbghelp.dll)"); \
    }

    DBGHELP_GET_PROC_ADDRESS(SymSetOptions);
    DBGHELP_GET_PROC_ADDRESS(SymInitialize);
    DBGHELP_GET_PROC_ADDRESS(SymCleanup);
    DBGHELP_GET_PROC_ADDRESS(SymLoadModuleEx);
    DBGHELP_GET_PROC_ADDRESS(SymUnloadModule64);
    DBGHELP_GET_PROC_ADDRESS(SymEnumSymbols);
    DBGHELP_GET_PROC_ADDRESS(SymGetModuleInfo64);
    DBGHELP_GET_PROC_ADDRESS(SymGetTypeInfo);

#undef  DBGHELP_GET_PROC_ADDRESS
}

// ----------------------------------------------------------------------------

CDbgHelp::~CDbgHelp()
{
    ::FreeLibrary(m_hModule);
}

// ----------------------------------------------------------------------------

CDbgHelp::PtrSymSetOptions CDbgHelp::GetSymSetOptions() const
{
    return m_pSymSetOptions;
}

// ----------------------------------------------------------------------------

CDbgHelp::PtrSymInitialize CDbgHelp::GetSymInitialize() const
{
    return m_pSymInitialize;
}

// ----------------------------------------------------------------------------

CDbgHelp::PtrSymCleanup CDbgHelp::GetSymCleanup() const
{
    return m_pSymCleanup;
}

// ----------------------------------------------------------------------------

CDbgHelp::PtrSymLoadModuleEx CDbgHelp::GetSymLoadModuleEx() const
{
    return m_pSymLoadModuleEx;
}

// ----------------------------------------------------------------------------

CDbgHelp::PtrSymUnloadModule64 CDbgHelp::GetSymUnloadModule64() const
{
    return m_pSymUnloadModule64;
}

// ----------------------------------------------------------------------------

CDbgHelp::PtrSymEnumSymbols CDbgHelp::GetSymEnumSymbols() const
{
    return m_pSymEnumSymbols;
}

// ----------------------------------------------------------------------------

CDbgHelp::PtrSymGetModuleInfo64 CDbgHelp::GetSymGetModuleInfo64() const
{
    return m_pSymGetModuleInfo64;
}

// ----------------------------------------------------------------------------

CDbgHelp::PtrSymGetTypeInfo CDbgHelp::GetSymGetTypeInfo() const
{
    return m_pSymGetTypeInfo;
}

// ----------------------------------------------------------------------------

PCSTR CDbgHelp::GetLibraryFileName()
{
    return "dbghelp.dll";
}

// ----------------------------------------------------------------------------

}   // namespace DSymLoad

// ----------------------------------------------------------------------------
